//
//  SZLDrawListViewController.m
//  Shengzhuli
//
//  Created by 卡忙科技 on 16/11/16.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "SZLDrawListViewController.h"
#import "SZLDrawTypeViewController.h"
#import "MyCountNetwork.h"
#import "SZLAccountHelper.h"
#import "MBProgressHUD+MJ.h"
#import "DrawListCell.h"
#import "SZLWithDrawViewController.h"
#import "ConDrawCountViewController.h"

@interface SZLDrawListViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *sourceTab;
@property (nonatomic,strong) NSMutableArray *draws;

@end

@implementation SZLDrawListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"提现";
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:self.sourceTab];
    [self getDrawLists];
}

- (void)createAddBtn
{
    UIButton *createBtn = [[UIButton alloc] initWithFrame:CGRectMake(25, ScreenH-64-20-40, ScreenW-50, 40)];
    [createBtn setTitle:@"+    新建账户" forState:UIControlStateNormal];
    [createBtn setBackgroundImage:GetImage(@"btn_bg_login_normal") forState:UIControlStateNormal];
    [createBtn setBackgroundImage:GetImage(@"btn_bg_login_normal") forState:UIControlStateHighlighted];
    createBtn.layer.cornerRadius = 5;
    createBtn.clipsToBounds = YES;
    [self.view addSubview:createBtn];
    [createBtn addTarget:self action:@selector(createClick) forControlEvents:UIControlEventTouchUpInside];
}
- (void)createClick
{
    SZLWithDrawViewController *drawVc = [[SZLWithDrawViewController alloc]init];
    drawVc.saveBlock = ^(){
        [self getDrawLists];
    };
    [self.navigationController pushViewController:drawVc animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.draws.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DrawListCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([DrawListCell class])];
    cell.model = self.draws[indexPath.section];
    __WEAK
    cell.btnClickBlock = ^(NSInteger tag, DrawListModel *model) {
        SZLWithDrawViewController *drawVc = [[SZLWithDrawViewController alloc]init];
        drawVc.typeModel = model;
        drawVc.saveBlock = ^(){
            [self getDrawLists];
        };
        [weakSelf.navigationController pushViewController:drawVc animated:YES];
    };
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ConDrawCountViewController *conVc = [[ConDrawCountViewController alloc]init];
    conVc.countModel = self.model;
    conVc.listModel = self.draws[indexPath.section];
    [self.navigationController pushViewController:conVc animated:YES];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self deleteDrawList:self.draws[indexPath.section]];
    [self.draws removeObjectAtIndex:indexPath.section];
    [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationTop];
}
#pragma mark ----------------------------
- (void)getDrawLists
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    [self showWaiter];
    [MyCountNetwork drawListsWithParame:parame response:^(BOOL success, NSString *msg, NSArray *lists) {
        [self hideWaiter];
        if (success) {
            if (lists.count == 0) {
                [self showNoDataLab:self.view range:CGRectMake(0, (ScreenH-64)*0.5-10, ScreenW, 20) title:@"暂无提现账户，请点击下方新建账户"];
            }else{
                [self hideNoDataLab];
                [self.draws removeAllObjects];
                [self.draws addObjectsFromArray:lists];
                [self.sourceTab reloadData];
            }
            [self createAddBtn];
        }else{
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}
- (void)deleteDrawList:(DrawListModel *)model
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"id"] = @(model.ID);
    parame[@"delete"] = @1;
    [self showWaiter];
    [MyCountNetwork deleteDrawListWithParame:parame response:^(BOOL success, NSString *msg) {
        [self hideWaiter];
        if (success) {
            [MBProgressHUD showSuccess:@"删除成功" toView:self.view];
            if (self.draws.count == 0) {
                [self showNoDataLab:self.view range:CGRectMake(0, self.sourceTab.centerY-50, ScreenW, 20) title:@"暂无提现账户，请点击下方新建账户"];
            }else{
                [self hideNoDataLab];
            }
        }else{
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}


- (UITableView *)sourceTab
{
    if (!_sourceTab) {
        _sourceTab = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH-64) style:UITableViewStyleGrouped];
        _sourceTab.contentInset = UIEdgeInsetsMake(0, 0, 60, 0);
        _sourceTab.delegate = self;
        _sourceTab.dataSource = self;
        _sourceTab.rowHeight = 135;
        _sourceTab.sectionFooterHeight = 0.01;
        [_sourceTab registerNib:[UINib nibWithNibName:NSStringFromClass([DrawListCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([DrawListCell class])];
        _sourceTab.tableFooterView = [[UIView alloc]init];
    }
    return _sourceTab;
}
- (NSMutableArray *)draws
{
    if (!_draws) {
        _draws = [NSMutableArray array];
    }
    return _draws;
}

@end
